package com.jit.monitor.model;

import android.content.Context;
import android.graphics.Color;
import android.util.Xml;

import com.jit.monitor.app.MyApp;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author crazyZhangxl on 2018/12/26.
 * Describe: 数据检测管理类
 */
public class CheckDataManager {
    private static final String CHECK_FILENAME = "checkinfo.xml";
    private static final HashMap<String, String> mTextEntry = new HashMap<>();
    private static final List<Integer> mColorList = new ArrayList<>();
    static {
        Context context = MyApp.getContext();
        loadInfo(context,CHECK_FILENAME);
        initColor();
    }

    public static HashMap<String, String> getTextEntry() {
        return mTextEntry;
    }

    public static void doInit(){

    }

    private static void initColor() {
        mColorList.add(Color.RED);
        mColorList.add(Color.BLUE);
        mColorList.add(Color.GRAY);
        mColorList.add(Color.GREEN);
        mColorList.add(Color.YELLOW);
        mColorList.add(Color.BLACK);
    }

    /**
     * 加载护具
     * @param context
     * @param checkFilename
     */
    private static void loadInfo(Context context, String checkFilename) {
        new EntryLoader().load(context,checkFilename);
    }

    public static int getColor(int position){
        if (position < mColorList.size()){
            return mColorList.get(position);
        }
        return Color.BLACK;
    }


    public static String getCheckChineseName(String english){
        if (mTextEntry.size() != 0){
            return mTextEntry.get(english);
        }
        return null;
    }


    public static String getEnglishName(String chineseName){
        Set<Map.Entry<String, String>> entries = mTextEntry.entrySet();
        Iterator<Map.Entry<String, String>> iterator = entries.iterator();
        while (iterator.hasNext()){
            Map.Entry<String, String> next = iterator.next();
            if (chineseName.equals(next.getValue())){
                return next.getKey();
            }
        }
        return null;
    }

    private static class EntryLoader extends DefaultHandler{
        void load(Context context,String assetPath){
            InputStream is = null;
            try {
                is = context.getAssets().open(assetPath);
                Xml.parse(is,Xml.Encoding.UTF_8,this);
            } catch (IOException e) {
                e.printStackTrace();
            }catch (SAXException e) {
                e.printStackTrace();
            }finally {
                if (is != null){
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
            if ("check".equals(localName)){
                String name = attributes.getValue(uri, "name");
                String value = attributes.getValue(uri, "value");
                mTextEntry.put(name,value);
            }
        }
    }

}
